
package com.ifcar.app.view;

import com.ifcar.app.activity.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.HeaderViewListAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class LoadingListView extends FrameLayout implements ListViewDelegator{
    
    private final static int STATE_INIT = 0;
    private final static int STATE_LOADING = STATE_INIT + 1;

    private ListViewDelegator mListView;

    private View mLoadingView;

    private View mEmptyView;

    private View mLoadingMoreView;
    
    private View mLoadingMoreClickView;

    private int mStart = 0;

    private int mTempStart = 0;
    
    private int mState = STATE_INIT;
    
    private boolean mIsMoreAdded;

    public LoadingListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public LoadingListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LoadingListView(Context context) {
        super(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        View list = findViewById(R.id.list);
        if (list instanceof ListViewDelegator) {
            mListView = (ListViewDelegator) list;
        } else if (list instanceof ListView) {
            mListView = createDelegator((ListView)list);
        } else {
            throw new IllegalArgumentException("R.id.list in LoadingListView only support ListViewDelegator or ListView");
        }

        mLoadingView = findViewById(R.id.loadinglist_loading);
        mEmptyView = LayoutInflater.from(getContext()).inflate(R.layout.common_list_loaderror, this, false);
        addView(mEmptyView);
        mListView.getListView().setEmptyView(mEmptyView);
    }
    
    public void reset() {
        mState = STATE_INIT;
        setViewVisibility(mListView, View.VISIBLE);
        setViewVisibility(mLoadingView, View.GONE);
        setViewEnabled(mLoadingMoreClickView, true);
        removeMoreView();
        mListView.getListView().setAdapter(null);
        setViewVisibility(mEmptyView, View.GONE);
    }

    public boolean inLoading() {
        return mState != STATE_INIT;
    }
    
    public ListView getListView() {
        return mListView.getListView();
    }
    
    public ListViewDelegator getListViewDelegator() {
        return mListView;
    }

    public View getLoadingView() {
        return mLoadingView;
    }
    
    public View getLoadingMoreView() {
        return mLoadingMoreView;
    }
    
    public View getLoadingMoreClickView() {
        return mLoadingMoreClickView;
    }

    public View getEmptyView() {
        return mEmptyView;
    }

    public int getStart() {
        return mStart;
    }
    
	public void setLoadingMoreView(View view, int clickViewId) {
		if (mLoadingMoreView == view) {
			return;
		}

		if (mIsMoreAdded && mLoadingMoreView != null) {
			removeMoreView();
		}
		mLoadingMoreView = view;
		mLoadingMoreClickView = null;

		if (view != null && clickViewId != 0) {
			mLoadingMoreClickView = view.findViewById(clickViewId);
		}
	}
	
	public void addMoreView() {
	    if (mIsMoreAdded || mLoadingMoreView == null) {
	        return;
	    }
	    mIsMoreAdded = true;
	    ListView listView = mListView.getListView();
	    
	    ListAdapter adapter = listView.getAdapter();
        if (adapter == null
                || (adapter instanceof HeaderViewListAdapter && ((HeaderViewListAdapter) adapter)
                        .getWrappedAdapter() == null)) {
            listView.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1,
                    new Object[] {}));
        }
	    listView.addFooterView(mLoadingMoreView, null, false);
	    if (adapter == null) {
	        listView.setAdapter(null);
	    }
	}
	
	public void removeMoreView() {
	    if (!mIsMoreAdded || mLoadingMoreView == null) {
	        return;
	    }
	    mIsMoreAdded = false;
        ListView listView = mListView.getListView();
        listView.removeFooterView(mLoadingMoreView);
	}

    public void startLoading(boolean isFirst) {
//        if (inLoading()) {
//            return;
//        }
        mState = STATE_LOADING;
        mTempStart = 0;
        setViewVisibility(mListView, isFirst ? View.GONE : View.VISIBLE);
        setViewVisibility(mLoadingView, isFirst ? View.VISIBLE : View.GONE);
        setViewVisibility(mEmptyView, View.GONE);
//        setViewEnabled(mLoadingMoreView, false);
    }

    /**
     * 加载完成需要刷新界面时调用
     * 
     * @param success 加载是否成功
     * @param hasMore 是否还有更多
     */
    public void completeLoading(boolean success, boolean hasMore) {
        mState = STATE_INIT;
        
        mStart = success ? mTempStart : mStart;
        boolean hasData = false;
        ListView listView = (mListView != null) ? mListView.getListView() : null;
        if (listView != null) {
            ListAdapter adapter = listView.getAdapter();
            hasData = adapter != null && !adapter.isEmpty();
        }
        setViewVisibility(mListView, hasData || success ? View.VISIBLE : View.GONE);
        setViewVisibility(mEmptyView, !hasData ? View.VISIBLE : View.GONE);
        setViewVisibility(mLoadingView, View.GONE);
        setViewEnabled(mLoadingMoreClickView, true);
    }

    public void startLoadingMore(int start) {
//        if (inLoading()) {
//            return;
//        }
        mState = STATE_LOADING;
        mTempStart = start;
        setViewVisibility(mListView, View.VISIBLE);
        setViewVisibility(mEmptyView, View.GONE);
        setViewVisibility(mLoadingView, View.GONE);
        setViewEnabled(mLoadingMoreClickView, false);
    }

    public void completeLoadingMore(boolean success, boolean hasMore) {
        mState = STATE_INIT;;
        mStart = success ? mTempStart : mStart;
        setViewVisibility(mListView, View.VISIBLE);
        setViewVisibility(mLoadingView, View.GONE);
        setViewEnabled(mLoadingMoreClickView, true);
    }

    private void setViewVisibility(ListViewDelegator delegator, int visibility) {
        if (delegator instanceof View) {
            setViewVisibility((View) delegator, visibility);
        } else {
            setViewVisibility(delegator.getListView(), visibility);
        }
    }
    
    private void setViewVisibility(View view, int visibility) {
        if (view != null) {
            view.setVisibility(visibility);
        }
    }
    
    private void setViewEnabled(View view, boolean enabled) {
        if (view != null) {
            view.setEnabled(enabled);
        }
    }
    
    private ListViewDelegator createDelegator(final ListView listView) {
        return new ListViewDelegator() {
            @Override
            public ListView getListView() {
                return listView;
            }
        };
    }
}
